//fs操作文件或文件夹的模块
const fs = require('fs')//导入fs模块
const http = require('http');//导入http模块
const url = require('url');//导入url模块
const path = require('path');
//目的：往text/01-requestDate文件中写入日志
//日志内容：根据信息记录请求的ip地址，和客户端的浏览器的信息，写日志
// 创建服务
let server = http.createServer((req, res) => {
    let ip = req.socket.remoteAddress;//获取请求客户端的IP地址
    console.log(ip); 
    let may=req.method//以获取请求的方法[ get ,post ]
    console.log(may);
    let _url=req.url//获取请求地址
    console.log(_url);
    let agentNews = req.headers['user-agent']//通过user-agent获取客户端浏览器的信息
    console.log(agentNews);
    //Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
    //文件路径
    let filePath = path.resolve('./text/01-requestDate.txt')
    //追加内容
    fs.appendFileSync(filePath, ip+'--'+' '+may+' '+_url+" "+"-"+" "+agentNews)
    res.end('Welcome to there')
   

}).listen(8080, () => { console.log('搭起服务端口了:http://localhost:8080'); })//监听服务端口;






